<?php
namespace Home\Controller;
use Think\Controller;
class MessageController extends Controller {
    public function index(){
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        $this->display(T('Home@Message/index'));
    }

    public function detail(){
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        if(!isset($_SESSION['uid'])) return $this->error('您尚未登录',U('User/login'),1);
        $self_url=$_SERVER['PATH_INFO'];
        $other_id=explode(".",end(explode("/",$self_url)))['0'];
//        dump($other_id);
//        dump($_POST);
//        dump($_SERVER);die();
        if(!is_numeric($other_id)) return $this->error('该私信不存在',U('User/info',array('id'=>$other_id)),1);
        $user=D('user')->getUserByUsername($_SESSION['uid']);
        $self_id=$user[0]['id'];
//        dump($other_id);
//        dump($self_id);die;
        $messages=D('Message')->getMessageByFromIdANDToId($other_id,$self_id);
//        dump($messages);die;
        if(!$messages) return $this->error('该私信不存在',U('User/info',array('id'=>$self_id)),1);
        foreach($messages as $key => $message){
            if($message['from_id']==$self_id){//自己的消息显示
                $messages[$key]['direction']='right';
                $from_avatar=D('Avatar')->getAvatarById($self_id);
                $messages[$key]['to_avatar']=$from_avatar[0]['name'];
            }
            else{
                $messages[$key]['direction']='left';
                $to_avatar=D('Avatar')->getAvatarById($other_id);
                $messages[$key]['other_avatar']=$to_avatar[0]['name'];
            }
        }
        $avatar=D('Avatar')->getAvatarById($self_id);
        $global_message['self_avatar']=$avatar[0]['name'];
        $global_message['from_id']=$self_id;
        $global_message['to_id']=$other_id;
        $global_message['id']=$other_id;
//        dump($messages);
//        dump($global_message);
        $this->assign('global_message',$global_message);
        $this->assign('messages',$messages);
        $this->display(T('Home@Message/detail'));

    }
    public function add(){
        //必须先判断是否登录!!!
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        if(IS_POST){
            if($_POST['form_name']!=null){
                $from_user=D('User')->getUserByUsername($_POST['from_name']);
                $_POST['from_id']=$from_user[0]['id'];
            }else{
                $from_user=D('User')->getUserByUsername($_SESSION['uid']);
                $_POST['from_id']=$from_user[0]['id'];
            }
//            dump($_POST);die();
            $message['from_id']=$_POST['from_id'];;
            $message['to_id']=$_POST['to_id'];
            $message['content']=$_POST['content'];
//            dump($message);die();
            $message_id=D('Message')->data($message)->add();
            if($message_id){
                $notice=array('from_id'=>$message['from_id'],'to_id'=>$message['to_id'],'notice_id'=>$message_id,'notice_type'=>'message');
                $notice_id=D('Notice')->data($notice)->add();
                if($notice_id){
                    return $this->success("私信成功",U('Message/detail',array('id'=>$message['to_id'])),1);
                }
                else {
                    D('Notice')->deleteNoticeById($notice_id);
                    D('Message')->deleteMessageById($message_id);
                    return $this->error("私信失败",U('Message/detail',array('id'=>$message['to_id'])),1);
                }
            }else{
                return $this->error("私信失败",U('Message/detail',array('id'=>$message['to_id'])),1);
            }

        }else
            return $this->error('私信失败',U('Message/detail',array('id'=>2)),1);
    }
}